<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(__FILE__)) . '/appwap.php');

$id = abs(intval($_GET['id']));
if (!$id || !$team = Table::FetchForce('team', $id) ) {
	redirect('index.php');
}

//echo $id.'@@@@';
/*收藏*/
if($login_user_id){
   $check_collect = Table::Fetch('collect', $id,'team_id');
	
	/*print_r($check_collect);
	echo $check_collect['user_id'].'@@@';
	echo $login_user_id;*/
	
	if($check_collect['user_id'] == $login_user_id){
		$is_collect = 1;
	}
}

$act = $_GET['act'];
if($act == 'collect'){
	if(!$login_user_id){
        redirect( WEB_ROOT . '/wap/login.php');
	}else{
	    $check_collect = Table::Fetch('collect', $id,'team_id');
		
		/*if($check_collect['user_id'] == $login_user_id){
		$is_collect = 1;
		}*/
		
		if($is_collect != 1){
			$collect['team_id'] = $team['id'];
			$collect['user_id'] = $login_user_id;
			$collect['title'] = $team['title'];
			$collect['time'] = time();
			$collect['team_price'] = $team['team_price'];
			$collect['image'] = $team['image'];
			
			$insert = array('team_id','user_id','title','time','team_price','image');
			
			$table = new Table('collect', $collect);
			if ( $table->insert($insert) ){
				Session::Set('notice', "收藏成功");
			   redirect( WEB_ROOT . "/wap/team.php?id={$id}");
			   
			}
		}
	}
}
if($act == 'delcollect'){
   if(Table::Delete('collect', $check_collect['id'])){
      redirect( WEB_ROOT . "/wap/team.php?id={$id}");
   }
}

/* refer */
if ($_rid = strval($_GET['r'])) { 
	$_rid = udecode($_rid);
	$_user = Table::Fetch('user',$_rid,'email');
	if($_user) cookieset('_rid', abs(intval($_user['id'])));
	redirect( WEB_ROOT . "/team.php?id={$id}");
}
$teamcity = Table::Fetch('category', $team['city_id']);
$city = $teamcity ? $teamcity : $city;
$city = $city ? $city : array('id'=>0, 'name'=>'全部');

$pagetitle = $team['title'];

$discount_price = $team['market_price'] - $team['team_price'];
$discount_rate = team_discount($team);

$left = array();
$now = time();

if($team['end_time']<$team['begin_time']){$team['end_time']=$team['begin_time'];}

$diff_time = $left_time = $team['end_time']-$now;
if ( $team['team_type'] == 'seconds' && $team['begin_time'] >= $now ) {
	$diff_time = $left_time = $team['begin_time']-$now;
}

$left_day = floor($diff_time/86400);
$left_time = $left_time % 86400;
$left_hour = floor($left_time/3600);
$left_time = $left_time % 3600;
$left_minute = floor($left_time/60);
$left_time = $left_time % 60;

/* progress bar size */
$bar_size = ceil(190*($team['now_number']/$team['min_number']));
$bar_offset = ceil(5*($team['now_number']/$team['min_number']));
$partner = Table::Fetch('partner', $team['partner_id']);
//print_r($partner);
$team['state'] = team_state($team);

/* your order */
if ($login_user_id && 0==$team['close_time']) {
	$order = DB::LimitQuery('order', array(
				'condition' => array(
					'team_id' => $id,
					'user_id' => $login_user_id,
					'state' => 'unpay',
					),
				'one' => true,
				));
}
/* end order */

/*kxx team_type */
if ($team['team_type'] == 'seconds') {
	die(include template('wap_team'));
}
if ($team['team_type'] == 'goods') {
	die(include template('wap_team'));
}
/*xxk*/
//商家服务评分体系
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'comment_grade' => '1',
	'team_id' => $id,
);
$count1 = Table::Count('order', $condition_m);
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'comment_grade' => '2',
	'team_id' => $id,
);
$count2 = Table::Count('order', $condition_m);
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'comment_grade' => '3',
	'team_id' => $id,
);
$count3 = Table::Count('order', $condition_m);
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'comment_grade' => '4',
	'team_id' => $id,
);
$count4 = Table::Count('order', $condition_m);
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'comment_grade' => '5',
	'team_id' => $id,
);
$count5 = Table::Count('order', $condition_m);
$count_mun = $count1+$count2+$count3+$count4+$count5;
$count5  =  number_format($count5/$count_mun*100,1);
$count4  =  number_format($count4/$count_mun*100,1);
$count3  =  number_format($count3/$count_mun*100,1);
$count2  =  number_format($count2/$count_mun*100,1);
$count1  =  number_format($count1/$count_mun*100,1);
$fwpf    =   number_format(($count5*5+$count4*4+$count3*3+$count2*2+$count1*1)/($count1+$count2+$count3+$count4+$count5),1);




//商品与网站描述是否相符

$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'shop_grade' => '1',
	'team_id' => $id,
);
$count11 = Table::Count('order', $condition_m);
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'shop_grade' => '2',
	'team_id' => $id,
);
$count22 = Table::Count('order', $condition_m);
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'shop_grade' => '3',
	'team_id' => $id,
);
$count33 = Table::Count('order', $condition_m);
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'shop_grade' => '4',
	'team_id' => $id,
);
$count44 = Table::Count('order', $condition_m);
$condition_m = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'shop_grade' => '5',
	'team_id' => $id,
);
$count55 = Table::Count('order', $condition_m);
$count_mun = $count11+$count22+$count33+$count44+$count55;
$count55  =  number_format($count55/$count_mun*100,1);
$count44  =  number_format($count44/$count_mun*100,1);
$count33  =  number_format($count33/$count_mun*100,1);
$count22  =  number_format($count22/$count_mun*100,1);
$count11  =  number_format($count11/$count_mun*100,1);
$spms    =   number_format((($count55*5+$count44*4+$count33*3+$count22*2+$count11*1)/($count11+$count22+$count33+$count44+$count55)),1);

$c_mun = $fwpf; 
$fwpf_stlye  =  $fwpf*20;


$condition = array(
    'state' => 'pay',
	'comment_display' => 'Y',
	"comment_content != 'Null'",
	'team_id' => $id,
);
$count = Table::Count('order', $condition);


list($pagesize, $offset, $pagestring) = pagestring($count, 8);

$orders = DB::LimitQuery('order', array(
	'condition' => $condition,
	'order' => 'ORDER BY comment_time DESC, id DESC',
	'size' => $pagesize,
	'offset' => $offset,
));

$i = 0;
foreach($orders as $vvv){
   
    $orders[$i]['comment_grade_tmp'] =  $vvv['comment_grade']*20; 
    $i++;
}


//print_r($orders);

$user_ids = Utility::GetColumn($orders, 'user_id');
$users = Table::Fetch('user', $user_ids);

$team_ids = Utility::GetColumn($orders, 'team_id');
$teams = Table::Fetch('team', $team_ids);

$ll = $partner['longlat'];
if ($ll) list($longi,$lati) = preg_split('/[,\s]+/',$ll,-1,PREG_SPLIT_NO_EMPTY);



/*seo*/
$seo_title = $team['seo_title'];
$seo_keyword = $team['seo_keyword'];
$seo_description = $team['seo_description'];
if($seo_title) $pagetitle = $seo_title;
/*end*/


/*附近的团购*/
$condition_fj = array( 
		'team_type' => 'normal',
		"begin_time < '{$now}'",
		"end_time > '{$now}'",
		"id in ({$team['fj_team_id']})",     
		);
	
$fjteams = DB::LimitQuery('team', array(
			'condition' => $condition_fj,
			'order' => 'ORDER BY  sort_order  DESC, id DESC',
));


 
$condition_mm = array(
	'partner_id' => $team['partner_id'],
	"id != {$team['id']}",
	'team_type' => 'normal',
	"begin_time < '{$now}'",
	"end_time > '{$now}'",
);
$count_mm = Table::Count('team', $condition_mm);

list($pagesize, $offset, $pagestring) = pagestring($count, 3);

$team_mm = DB::LimitQuery('team', array(
	'condition' => $condition_mm,
	//'order' => 'ORDER BY comment_time DESC, id DESC',
	//'size' => $pagesize,
	//'offset' => $offset,
));

$current_hover = 'class="current"';
//写入浏览记录
$id =  $team['id'];
$name  =  $team['title'];
$pic  =  $team['image'];
$team_price  =  $team['team_price'];
$mark_price  =  $team['market_price'];
$mun  =  $team['now_number'];

zuijinborwe($id,$name,$pic,$team_price,$mark_price,$mun);
include template('wap_team');
